package com.kaiyidiannao.activity.login_or_registered;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.kaiyidiannao.common.BaseActionBarActivity;
import com.kaiyidiannao.common.util.AppUtils;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.data.Validate;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.req.model.GetRegister;
import com.kaiyidiannao.model.req.model.GetResigterCode;
import com.taplinker.core.util.LogUtil;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by wzg on 14-4-14.
 */
public class UserRegistered extends BaseActionBarActivity implements Observer {

    private ImageButton registered;
    private EditText phoneNum;
    private EditText email;
    private EditText registered_password;
    private EditText registered_password_again;
    private EditText nickName;
    private EditText validate_code;
    private ImageButton sentNum;
    String uphone;
    String uemail;
    String uregistered_password;
    String uregistered_password_again;
    String uname;
    String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_registered);
        AppUtils.setupUI(findViewById(R.id.u_registered), this);
        initID();
        // 添加观察者
        DataCenter.getInstance().addObserver(this);
//        phoneNum.setInputType(InputType.TYPE_CLASS_NUMBER)


        sentNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phoneNum.getText().toString() != null && !"".equals(phoneNum.getText().toString())) {
                    if (phoneNum.getText().toString().length() == 11) {
                        showProgressDialog();
                        new GetResigterCode(phoneNum.getText().toString(), "1").execute();
                    } else {
                        AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                .setMessage("手机号为11位,只能输入数字,不能输入空格").setPositiveButton("确定", null).show();
                    }
                } else {
                    AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                            .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
                }


            }
        });

        registered.setOnClickListener(new View.OnClickListener()

                                      {
                                          @Override
                                          public void onClick(View v) {
                                              uphone = phoneNum.getText().toString();
                                              Log.d("uphone", uphone);
                                              code = validate_code.getText().toString();
                                              uemail = email.getText().toString();
                                              uregistered_password = registered_password.getText().toString();
                                              uregistered_password_again = registered_password_again.getText().toString();
                                              uname = nickName.getText().toString();

                                              if (uphone != null && !"".equals(uphone)) {
                                                  if (uphone.length() == 11) {
                                                      if (phoneNum.getText().toString().matches(Validate.phoneValidate)) {
                                                          if (code != null && !"".equals("code")) {
                                                              if (code.length() == 6) {
                                                                  if (uname != null && !"".equals(uname)) {
                                                                      if (uname.length() <= 8 && uname.length() >= 2) {
                                                                          if (Validate.search(uname)) {
                                                                              if (email.length() == 0 || email == null) {
                                                                                  if (uregistered_password != null && !"".equals(uregistered_password)) {
                                                                                      if (uregistered_password.length() <= 20 && uregistered_password.length() >= 6) {
                                                                                          if (registered_password.getText().toString().matches(Validate.passwordValidate)) {
                                                                                              if (uregistered_password_again != null && !"".equals(uregistered_password_again)) {
                                                                                                  if (registered_password.getText().toString().equals(uregistered_password_again)) {
                                                                                                      showProgressDialog("正在注册");
                                                                                                      Log.d("dsadasdsa", uphone + uemail + uregistered_password);
                                                                                                      new GetRegister(uphone, uname, uemail, uregistered_password, "1",code).execute();
                                                                                                  } else {
                                                                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                              .setMessage("请检查两次密码是否一样").setPositiveButton("确定", null).show();
                                                                                                  }

                                                                                              } else {
                                                                                                  AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                          .setMessage("再次输入密码不能为空").setPositiveButton("确定", null).show();
                                                                                              }


                                                                                          } else {
                                                                                              AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                      .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                                          }
                                                                                      } else {
                                                                                          AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                  .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                                      }

                                                                                  } else {
                                                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                              .setMessage("密码不能为空").setPositiveButton("确定", null).show();
                                                                                  }
                                                                              } else {
                                                                                  if (email.getText().toString().matches(Validate.emailValidate)) {
                                                                                      if (uregistered_password != null && !"".equals(uregistered_password)) {
                                                                                          if (uregistered_password.length() <= 20 && uregistered_password.length() >= 6) {
                                                                                              if (registered_password.getText().toString().matches(Validate.passwordValidate)) {
                                                                                                  if (uregistered_password_again != null && !"".equals(uregistered_password_again)) {
                                                                                                      if (registered_password.getText().toString().equals(uregistered_password_again)) {
                                                                                                          showProgressDialog("正在注册");
                                                                                                          Log.d("dsadasdsa", uphone + uemail + uregistered_password);
                                                                                                          new GetRegister(uphone, uname, uemail, uregistered_password, "1",code).execute();
                                                                                                      } else {
                                                                                                          AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                                  .setMessage("请检查两次密码是否一样").setPositiveButton("确定", null).show();
                                                                                                      }

                                                                                                  } else {
                                                                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                              .setMessage("再次输入密码不能为空").setPositiveButton("确定", null).show();
                                                                                                  }


                                                                                              } else {
                                                                                                  AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                          .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                                              }
                                                                                          } else {
                                                                                              AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                      .setMessage("密码为6-20位，由数字和字母组合而成，不能输入空格").setPositiveButton("确定", null).show();
                                                                                          }

                                                                                      } else {
                                                                                          AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                                  .setMessage("密码不能为空").setPositiveButton("确定", null).show();
                                                                                      }
                                                                                  } else {
                                                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                              .setMessage("请检查邮箱是否正确").setPositiveButton("确定", null).show();
                                                                                  }
                                                                              }

                                                                          } else {
                                                                              AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                      .setMessage("昵称为2-8位，不能输入空格").setPositiveButton("确定", null).show();
                                                                          }
                                                                      } else {
                                                                          AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                                  .setMessage("昵称为2-8位，不能输入空格").setPositiveButton("确定", null).show();
                                                                      }

                                                                  } else {
                                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                              .setMessage("昵称不能为空").setPositiveButton("确定", null).show();
                                                                  }
                                                              } else {
                                                                  AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                          .setMessage("验证码长度应为6位").setPositiveButton("确定", null).show();
                                                              }
                                                          } else {
                                                              AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                      .setMessage("验证码不能为空").setPositiveButton("确定", null).show();
                                                          }


                                                      } else {
                                                          AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                                  .setMessage("手机号为11位,只能输入数字,不能输入空格").setPositiveButton("确定", null).show();
                                                      }

                                                  } else {
                                                      AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                              .setMessage("手机号为11位,只能输入数字,不能输入空格").setPositiveButton("确定", null).show();
                                                  }
                                              } else {
                                                  AlertDialog builder = new AlertDialog.Builder(UserRegistered.this).setTitle("错误信息")
                                                          .setMessage("手机号不能为空").setPositiveButton("确定", null).show();
                                              }

                                          }
                                      }
        );

    }

    public void initID() {
        phoneNum = (EditText) findViewById(R.id.phoneNum);
        email = (EditText) findViewById(R.id.email);
        registered_password = (EditText) findViewById(R.id.registered_password);
        registered = (ImageButton) findViewById(R.id.registered);
        registered_password_again = (EditText) findViewById(R.id.registered_password_again);
        nickName = (EditText) findViewById(R.id.nickName);
        validate_code = (EditText) findViewById(R.id.validate_code);
        sentNum = (ImageButton) findViewById(R.id.sentNum);
    }

    @Override
    public void update(Observable observable, Object data) {
        DataCenter.flag flag = (DataCenter.flag) data;
        String code = DataCenter.code;
        closeProgressDialog();
        if (flag == DataCenter.flag.GET_REGISTER_SUCCESS
                || flag == DataCenter.flag.GET_REGISTER_FAILED) { //类表数据
            if (flag == DataCenter.flag.GET_REGISTER_SUCCESS) {
                if (code.equals("1")) {
                    closeProgressDialog();
                    Toast.makeText(UserRegistered.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(UserRegistered.this, LoginOrRegistered.class);
                    startActivity(intent);
                    finish();
                } else {
                    closeProgressDialog();
                    Toast.makeText(UserRegistered.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
                }
            } else {
                closeProgressDialog();
                //Toast.makeText(UserRegistered.this, DataCenter.msg, Toast.LENGTH_SHORT).show();
            }
        } else {
            //Toast.makeText(UserRegistered.this, "网络连接错误，请稍后重试", Toast.LENGTH_SHORT).show();
        }

        if (flag == DataCenter.flag.GET_REGISTER_CODE_SUCCESS || flag == DataCenter.flag.GET_REGISTER_CODE_FAILED) {
            if (flag == DataCenter.flag.GET_REGISTER_CODE_SUCCESS) {
                closeProgressDialog();
                showToast(DataCenter.msg);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        LogUtil.d(DataCenter.LOG_INFO, UserRegistered.class.getSimpleName() + ".onDestroy");
        //删除观察者
        DataCenter.getInstance().deleteObserver(this);
    }
}
